package part3.c14_strategy;

/**
 * Created by lie on 2018/3/23.
 * 里头的数据用接口来操作
 * 这样就避免了数据直接和算法关联，从而解耦
 */
public class BubbleSorter {
    private int operations;
    private int length = 0;
    private SortHandle iSortHandle;

    public BubbleSorter(SortHandle iSortHandle) {
        this.iSortHandle = iSortHandle;
    }

    public int sort(Object array){
        iSortHandle.setArray(array);
        this.length = iSortHandle.length();
        if (length <= 1)
            return operations;

        for (int lastIndex= length-2;lastIndex>=0; lastIndex--){
            for (int begin=0;begin<=lastIndex;begin++){
                if (iSortHandle.isNeedSwap(begin))
                    iSortHandle.swap(begin);
                operations++;
            }
        }

        return operations;
    }

}
